﻿using System;
using System.Collections.Generic;
using System.Text;
using Fiddler;
using System.Windows.Forms;

namespace TangoViewExtension
{
   public class WebViewer : Inspector2, IRequestInspector2
   {

      RequestDisplayControl requestDisplayControl;
      private byte[] m_entityBody;
      HTTPRequestHeaders m_Headers;
      private bool m_bDirty;

      private bool m_bReadOnly;

      public bool bReadOnly
      {
         get
         {
            return m_bReadOnly;
         }
         set
         {
            m_bReadOnly = value;   // TODO: You probably also want to turn your visible control GRAY (false) or WHITE (true) here depending on the value being passed in.   
         }
      }

      public HTTPRequestHeaders headers
      {
         get
         {
            return null;    // Return null if your control doesn't allow header editing.
         }
         set
         {
             m_Headers = value;
         }
      }

      public byte[] body
      {
         get
         {
            return m_entityBody;
         }
         set
         {
            m_entityBody = value;
            string content_type = "";
            if (m_Headers != null)
                content_type = m_Headers["Content-Type"];
            requestDisplayControl.Display(System.Text.Encoding.UTF8.GetString(value), content_type); // TODO: Use correct encoding based on content header?
            m_bDirty = false;   // Note: Be sure to have an OnTextChanged handler for the textbox which sets m_bDirty to true!
         }
      }

      public bool bDirty
      {
         get
         {
            return m_bDirty;
         }
      }

      public override void AddToTab(System.Windows.Forms.TabPage o)
      {
         requestDisplayControl = new RequestDisplayControl();
         o.Text = "TangoXmlView";
         o.Controls.Add(requestDisplayControl);
         o.Controls[0].Dock = DockStyle.Fill;
      }

      public override int GetOrder()
      {
         return 0;
      }

      public void Clear()
      {
         m_entityBody = null;
         requestDisplayControl.txtBody.Clear();
      }

   }
}